@echo off
setlocal enabledelayedexpansion

:: UBBC服务图片检查测试脚本 (Windows版本)
:: 使用curl调用RESTful API进行图片合规性检查

:: 默认配置
set "DEFAULT_SERVER_URL=http://localhost:8080"
set "DEFAULT_IMAGE_URL=https://ark-project.tos-cn-beijing.ivolces.com/images/view.jpeg"

:: 初始化变量
set "SERVER_URL=%DEFAULT_SERVER_URL%"
set "IMAGE_URL="
set "USER_ID="
set "VERBOSE=false"
set "TIMEOUT=30"
set "API_KEY="
set "SHOW_HELP=false"

:: 解析命令行参数
:parse_args
if "%~1"=="" goto validate_args
if "%~1"=="-h" set "SHOW_HELP=true" && goto next_arg
if "%~1"=="--help" set "SHOW_HELP=true" && goto next_arg
if "%~1"=="-s" set "SERVER_URL=%~2" && shift && goto next_arg
if "%~1"=="--server" set "SERVER_URL=%~2" && shift && goto next_arg
if "%~1"=="-u" set "USER_ID=%~2" && shift && goto next_arg
if "%~1"=="--user-id" set "USER_ID=%~2" && shift && goto next_arg
if "%~1"=="-v" set "VERBOSE=true" && goto next_arg
if "%~1"=="--verbose" set "VERBOSE=true" && goto next_arg
if "%~1"=="-t" set "TIMEOUT=%~2" && shift && goto next_arg
if "%~1"=="--timeout" set "TIMEOUT=%~2" && shift && goto next_arg
if "%~1"=="-k" set "API_KEY=%~2" && shift && goto next_arg
if "%~1"=="--api-key" set "API_KEY=%~2" && shift && goto next_arg
if "%~1:~0,1"=="-" (
    echo 错误: 未知选项 %~1
    echo 使用 %~nx0 --help 查看帮助信息
    exit /b 1
)
if "!IMAGE_URL!"=="" (
    set "IMAGE_URL=%~1"
) else (
    echo 错误: 只能指定一个图片URL
    exit /b 1
)
:next_arg
shift
goto parse_args

:validate_args
:: 显示帮助信息
if "%SHOW_HELP%"=="true" (
    echo Usage: %~nx0 [OPTIONS] IMAGE_URL
    echo.
    echo 测试UBBC服务的图片检查功能
    echo.
    echo OPTIONS:
    echo   -h, --help              显示帮助信息
    echo   -s, --server URL        指定服务器地址 ^(默认: %DEFAULT_SERVER_URL%^)
    echo   -u, --user-id ID        指定用户ID
    echo   -v, --verbose           显示详细输出
    echo   -t, --timeout SECONDS   设置请求超时时间 ^(默认: 30秒^)
    echo   -k, --api-key KEY       设置API密钥
    echo.
    echo EXAMPLES:
    echo   %~nx0 https://example.com/image.jpg
    echo   %~nx0 -s http://192.168.1.100:8080 -u user123 https://example.com/image.jpg
    echo   %~nx0 -v -t 60 https://example.com/large-image.jpg
    echo.
    exit /b 0
)

:: 验证参数
if "%IMAGE_URL%"=="" (
    echo 错误: 必须提供图片URL
    echo 使用 %~nx0 --help 查看帮助信息
    exit /b 1
)

:: 验证URL格式
echo %IMAGE_URL% | findstr /r "^https\?://" >nul
if errorlevel 1 (
    echo 错误: 图片URL必须以http://或https://开头
    exit /b 1
)

:: 构建请求体
set "REQUEST_BODY={\"image_url\": \"%IMAGE_URL%\""
if not "%USER_ID%"=="" (
    set "REQUEST_BODY=!REQUEST_BODY!, \"user_id\": \"%USER_ID%\""
)
set "REQUEST_BODY=!REQUEST_BODY!}"

:: 构建curl命令
set "CURL_CMD=curl -X POST"
set "CURL_CMD=!CURL_CMD! -H \"Content-Type: application/json\""

:: 添加API密钥（如果提供）
if not "%API_KEY%"=="" (
    set "CURL_CMD=!CURL_CMD! -H \"X-API-Key: %API_KEY%\""
)

set "CURL_CMD=!CURL_CMD! -d \"!REQUEST_BODY!\""
set "CURL_CMD=!CURL_CMD! --connect-timeout %TIMEOUT%"
set /a "MAX_TIME=%TIMEOUT% + 10"
set "CURL_CMD=!CURL_CMD! --max-time !MAX_TIME!"

:: 添加详细输出选项
if "%VERBOSE%"=="true" (
    set "CURL_CMD=!CURL_CMD! -v"
) else (
    set "CURL_CMD=!CURL_CMD! -s"
)

set "CURL_CMD=!CURL_CMD! \"%SERVER_URL%/api/v1/check-image\""

:: 显示请求信息
echo ============================================
echo UBBC 图片检查测试
echo ============================================
echo 服务器地址: %SERVER_URL%
echo 图片URL:    %IMAGE_URL%
if not "%USER_ID%"=="" echo 用户ID:     %USER_ID%
echo 超时时间:   %TIMEOUT%秒
echo ============================================

if "%VERBOSE%"=="true" (
    echo 请求体:
    echo !REQUEST_BODY!
    echo ============================================
)

:: 检查curl是否可用
curl --version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到curl命令
    echo 请安装curl或确保curl在PATH环境变量中
    exit /b 1
)

:: 执行请求
echo 发送请求中...
for /f "delims=" %%i in ('echo %time%') do set "START_TIME=%%i"

:: 创建临时文件存储响应
set "TEMP_FILE=%TEMP%\ubbc_response_%RANDOM%.json"

:: 执行curl命令
!CURL_CMD! > "%TEMP_FILE%" 2>&1
set "HTTP_STATUS=!errorlevel!"

for /f "delims=" %%i in ('echo %time%') do set "END_TIME=%%i"

echo ============================================
echo 响应
echo ============================================

:: 检查curl命令是否成功执行
if !HTTP_STATUS! neq 0 (
    echo 错误: 请求失败 ^(curl exit code: !HTTP_STATUS!^)
    if !HTTP_STATUS!==6 echo 原因: 无法解析主机名
    if !HTTP_STATUS!==7 echo 原因: 无法连接到服务器
    if !HTTP_STATUS!==28 echo 原因: 请求超时
    if !HTTP_STATUS! gtr 28 echo 原因: 网络错误或服务器不可达
    if exist "%TEMP_FILE%" del "%TEMP_FILE%"
    exit /b 1
)

:: 显示响应内容
if exist "%TEMP_FILE%" (
    type "%TEMP_FILE%"
    echo.
    
    :: 简单检查响应内容
    findstr /c:"\"合规\":true" "%TEMP_FILE%" >nul
    if not errorlevel 1 (
        echo ============================================
        echo ✅ 图片内容合规
        del "%TEMP_FILE%"
        exit /b 0
    )
    
    findstr /c:"\"合规\":false" "%TEMP_FILE%" >nul
    if not errorlevel 1 (
        echo ============================================
        echo ❌ 图片内容违规
        del "%TEMP_FILE%"
        exit /b 2
    )
    
    findstr /c:"\"compliant\":true" "%TEMP_FILE%" >nul
    if not errorlevel 1 (
        echo ============================================
        echo ✅ 图片内容合规
        del "%TEMP_FILE%"
        exit /b 0
    )
    
    findstr /c:"\"compliant\":false" "%TEMP_FILE%" >nul
    if not errorlevel 1 (
        echo ============================================
        echo ❌ 图片内容违规
        del "%TEMP_FILE%"
        exit /b 2
    )
    
    findstr /c:"\"error\"" "%TEMP_FILE%" >nul
    if not errorlevel 1 (
        echo ============================================
        echo ❌ 请求出错
        del "%TEMP_FILE%"
        exit /b 1
    )
    
    echo ============================================
    echo ⚠️  无法解析响应
    del "%TEMP_FILE%"
    exit /b 3
) else (
    echo 错误: 无法读取响应
    exit /b 1
)
